package util.concurrent;

import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author Khai Loon
 * @version 2017/4/6 0006 上午 10:53
 */
public class Join {

    /**
     * Thread.join 源码分析
     * public final synchronized void join(){ //获取Thread对象锁
     * if (millis == 0) {
     * while (isAlive()) {   //是存活
     * wait(0);          //释放对象锁，等到 获取到对象锁的线程调用 notify
     * }
     * }
     * }
     */
    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t1 完了!");
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2 完了!");
        });
        t2.start();

        t2.join();
        System.out.println("t2 真的完了");
        t1.join();//强制等待t1 运行完毕  类似于回调线程   todo 主线程调用 t1 join wait 方法->等待状态

        Thread.currentThread().join();

    }

}
